import stringify from '@/utils/querystring';

import router from './router';
import cookie from './cookie';
import { useMainStore } from '@/store/store';

export const getFoodName = (name) => {
  return name;
  // const main = useMainStore();
  // const nameInfo = name?.split(',');
  // if (nameInfo?.length !== 3) return name;
  // if (main.currentLanguage.key == 'zh') return nameInfo[0];
  // if (main.currentLanguage.key == 'en') return nameInfo[2];
  // else return nameInfo[1];
};

export const getLocalCurrencyInfo = (price) => {
  const main = useMainStore();
  console.log(main.currencySymbol, main.rate, '---');
  if (main.currencySymbol === '') return '';
  return main.isAtFront
    ? '(' + main.currencySymbol + ' ' + price * main.rate + ')'
    : '(' + price * main.rate + ' ' + main.currencySymbol + ')';
};

export const handleLoginFailure = () => {
  uni.removeStorageSync('userinfo');
  uni.removeStorageSync('accessToken');
  const main = useMainStore();
  main.SET_MEMBER({});
  main.SET_TOKEN('');
  // #ifdef H5
  if (isWeixin()) {
    uni.switchTab({
      url: '/pages/index/index',
    });

    return;
  }
  // #endif
  uni.redirectTo({
    url: '/pages/components/pages/login/login',
  });
};

export function parseUrl(location) {
  if (typeof location === 'string') return location;
  const { url, query } = location;

  const queryStr = stringify(query);

  if (!queryStr) {
    return url;
  }

  return `${url}?${queryStr}`;
}

const toAuth = () => {
  uni.showToast({
    title: '暂未开放',
    icon: 'none',
    duration: 2000,
  });
};

export default {
  install: (app, options) => {
    // 在这里编写插件代码
    // 注入一个全局可用的 $translate() 方法
    app.config.globalProperties.$yrouter = router;
    app.config.globalProperties.$cookie = cookie;
    app.config.globalProperties.$toAuth = toAuth;
    app.config.globalProperties.$onClickLeft = () => {
      router.back();
      //uni.navigateBack()
      //const mypage = getCurrentPages()
      //console.log('mypage:',mypage)
    };

    // #ifdef H5
    app.config.globalProperties.$platform = 'h5';
    // #endif

    // #ifdef APP-PLUS
    // app端
    app.config.globalProperties.$platform = 'app';
    // #endif

    // #ifdef MP-WEIXIN
    app.config.globalProperties.$platform = 'routine';
    // #endif
  },
};
